{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Google Translate API for Python.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyMhSVFXn98D4SgMjeHmqqKv",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Google_Translate_API_for_Python.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4unOk6cs2NKQ",
        "colab_type": "text"
      },
      "source": [
        "# Google Translate API for Python\n",
        "\n",
        "## In this tutorial. I will demonstrate how to use the Google Translate API for translating the data from Hindi to English.\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1262/1*R5U_GWCePCS_S7IczC4BMw.png)*italicized text*"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fp0q3l6N2cMT",
        "colab_type": "text"
      },
      "source": [
        "There are some things I want to clear at the beginning of the tutorial. First, I would translate the data from [Hindi](https://en.wikipedia.org/wiki/Hindi) to [English](https://en.wikipedia.org/wiki/English_language). Also, I will demonstrate the translation on a [Pandas Data Frame](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html), meaning I will convert the entire data frame from Hindi to English. Isn’t that great? Well, let’s get started. Also, you can use the [Google Colab notebook](https://colab.research.google.com/) to type the code. I would recommend everyone go through the documentation of the Google Translate API for Python completely, so by the time you code you would probably know what I mean. It would be a cakewalk because you would know most of the terms while coding.\n",
        "\n",
        "[Google Translate API official documentation](https://pypi.org/project/googletrans/)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Ev_UbeyR23Sj",
        "colab_type": "text"
      },
      "source": [
        "Open your Google Colab and create a new notebook and name it as “***Google _Translations.ipynb***”. Before actually typing the code I want all of you guys manually install the Google Translate library inside the notebook. So to do this just type `!pip install googletrans` . This command automatically downloads and installs the library as shown below:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PY5aV_ikp9U1",
        "colab_type": "code",
        "outputId": "a5f5d902-d05f-47f2-b2a4-7eae2694ac85",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 279
        }
      },
      "source": [
        "# install googletrans using pip\n",
        "!pip install googletrans"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting googletrans\n",
            "  Downloading https://files.pythonhosted.org/packages/fd/f0/a22d41d3846d1f46a4f20086141e0428ccc9c6d644aacbfd30990cf46886/googletrans-2.4.0.tar.gz\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from googletrans) (2.21.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->googletrans) (2019.11.28)\n",
            "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->googletrans) (3.0.4)\n",
            "Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->googletrans) (1.24.3)\n",
            "Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->googletrans) (2.8)\n",
            "Building wheels for collected packages: googletrans\n",
            "  Building wheel for googletrans (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for googletrans: filename=googletrans-2.4.0-cp36-none-any.whl size=15777 sha256=a4ab48fed197c07033edeeae554cc0af78b6ec06d98459b65828509ddd763e59\n",
            "  Stored in directory: /root/.cache/pip/wheels/50/d6/e7/a8efd5f2427d5eb258070048718fa56ee5ac57fd6f53505f95\n",
            "Successfully built googletrans\n",
            "Installing collected packages: googletrans\n",
            "Successfully installed googletrans-2.4.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mNqgTBII3D9w",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9MOyJFT_3FY9",
        "colab_type": "text"
      },
      "source": [
        "## Importing the necessary libraries\n",
        "\n",
        "In this step, we will be importing the necessary libraries that we will be using throughout the tutorial. The library ***pandas*** are for storing the CSV data into a data frame. And ***googletrans*** is obviously used for translation and we will also use one of its methods called ***Translator*** which you will see in the later tutorial.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Dmxp7yNVqHKH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Importing the necessary libraries\n",
        "import pandas as pd\n",
        "import googletrans\n",
        "from googletrans import Translator"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Z3u32t703TYb",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5WfwlTR43UOb",
        "colab_type": "text"
      },
      "source": [
        "## Storing the CSV file as a data frame\n",
        "\n",
        "In this step, we will be storing the CSV file as a data frame with the help of pandas. For you to get the CSV file, click the link below:\n",
        "\n",
        "[Dataset download](https://drive.google.com/open?id=1UZl-vf7uBh80GaQFk1-kxsa_zI-_rwFm)\n",
        "\n",
        "\n",
        "After downloading the CSV file, upload the file to Google Colab. There are 3 horizontal lines on the left-hand side, on hovering it prompts “Show table of contents”. After clicking go the “Files” tab and then press “Upload”. Then upload the CSV.\n",
        "\n",
        "![alt text](https://miro.medium.com/max/3348/1*tnu_udxIon2cupBK7P8G6A.png)\n",
        "\n",
        "\n",
        "Now you have to read the CSV file and store it in a data frame. For clarity, I am displaying the first 10- rows of the data frame.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IRTLLWcNqLvZ",
        "colab_type": "code",
        "outputId": "f4004663-cf18-48f9-98b1-d7f368c1faaa",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 362
        }
      },
      "source": [
        "# Reading and storing the CSV file as a dataframe\n",
        "df = pd.read_csv('/content/Vegetables_names_in_hindi.csv')\n",
        "df.head(10)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Vegetable Names</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>गाजर</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>शिमला मिर्च</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>भिन्डी</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>मक्का</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>लाल मिर्च</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>खीरा</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>कढ़ी पत्ता</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>बैगन</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>लहसुन</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>अदरक</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  Vegetable Names\n",
              "0           गाजर \n",
              "1     शिमला मिर्च\n",
              "2         भिन्डी \n",
              "3          मक्का \n",
              "4       लाल मिर्च\n",
              "5           खीरा \n",
              "6       कढ़ी पत्ता\n",
              "7           बैगन \n",
              "8          लहसुन \n",
              "9           अदरक "
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pFv5EEzk3pcj",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Z_n1__IB3qGU",
        "colab_type": "text"
      },
      "source": [
        "## Building the translator function and translating the data frame\n",
        "\n",
        "This is where we actually translate the data frame from Hindi to English. The original code was retrieved from an [article](https://towardsdatascience.com/translate-a-pandas-data-frame-using-googletrans-library-fb0aa7fca592). But I made some changes here and there. So to know more about translation function click [here](https://towardsdatascience.com/translate-a-pandas-data-frame-using-googletrans-library-fb0aa7fca592).\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f2biUUbJqa6Z",
        "colab_type": "code",
        "outputId": "f99d5d83-e940-431f-d596-4ad03c176688",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 190
        }
      },
      "source": [
        "translator = Translator()\n",
        "translations = {}\n",
        "for column in df.columns:\n",
        "    # Unique elements of the column\n",
        "    unique_elements = df[column].unique()\n",
        "    for element in unique_elements:\n",
        "        # Adding all the translations to a dictionary (translations)\n",
        "        translations[element] = translator.translate(element).text\n",
        "translations"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "{'अदरक ': 'Ginger',\n",
              " 'कढ़ी पत्ता': 'Curry leaves',\n",
              " 'खीरा ': 'Cucumber',\n",
              " 'गाजर ': 'carrot',\n",
              " 'बैगन ': 'Brinjal',\n",
              " 'भिन्डी ': 'Lady finger',\n",
              " 'मक्का ': 'Corn',\n",
              " 'लहसुन ': 'Garlic',\n",
              " 'लाल मिर्च': 'Red chilly',\n",
              " 'शिमला मिर्च': 'capsicum'}"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Jx63ndx34EqH",
        "colab_type": "text"
      },
      "source": [
        "Basically, I am storing all the unique elements in a data frame and then translating every element into English with the help of the translator function (Translator()). By doing so, as you can see from the above output, all the elements are now being translated from Hindi to English which is stored in a dictionary.\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oh4sfy5o4HNU",
        "colab_type": "text"
      },
      "source": [
        "## Replacing the translated words to the original data frame\n",
        "\n",
        "Now the final step is to replace or save the translated data into a new or the original data frame. Here I would be replacing it with the original data frame with the help of the [pandas replace function](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html).\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1yMLx4_SqisT",
        "colab_type": "code",
        "outputId": "f7062280-9089-44f3-a893-52d14a1e616b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 359
        }
      },
      "source": [
        "# Replacing all the translated words from the dictionary to the original dataframe\n",
        "\n",
        "df.replace(translations, inplace = True)\n",
        "df.head(10)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Vegetable Names</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>carrot</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>capsicum</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>Lady finger</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>Corn</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Red chilly</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>Cucumber</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>Curry leaves</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>Brinjal</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>Garlic</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>Ginger</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  Vegetable Names\n",
              "0          carrot\n",
              "1        capsicum\n",
              "2     Lady finger\n",
              "3            Corn\n",
              "4      Red chilly\n",
              "5        Cucumber\n",
              "6    Curry leaves\n",
              "7         Brinjal\n",
              "8          Garlic\n",
              "9          Ginger"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7qFIzK954OpN",
        "colab_type": "text"
      },
      "source": [
        "You have successfully translated the data from the CSV file from the Hindi language to English. In the future, you can use this tutorial as a reference to translate the data from a different language to English by default. I hope you guys learned something new today. If you have any doubts about the tutorial, don’t hesitate to ask in the comment section below. Until then, see you next time. Goodbye.\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    }
  ]
}